package pers.zdb.ag.util;

import com.jfinal.kit.HttpKit;

/**
 * @Description:
 * @author: Adobe Chow
 * @date: 2018/8/8 17:44
 * @Copyright: www.winshang.com Inc. All rights reserved.
 */
public class HttpUtils {

    public static void main(String[] args) {
        System.out.println(getAndTry("http://www.baidu.com2",5,10));
    }

    public static String getAndTry(String url){
        return getAndTry(url,10,10);
    }

    /**
     * 如果http get 获取失败则停顿tryTime*millionSecond毫秒后再重新获取(自动尝试tryTime次)
     * @return
     */
    public static String getAndTry(String url,int tryTime,int millionSecond){
        String result = null;

        if (tryTime<=0){
            return result;
        }

        try {
            result = HttpKit.get(url);
            return result;
        }catch (Exception e){

            result = getAndTry(url,(tryTime-1),tryTime*millionSecond);

            try {
                Thread.sleep(millionSecond*tryTime);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

        }

        return result;
    }

}
